package runtime

import (
	"fmt"
	"gitee.com/injoyai/goutil/string/logs/v1"
	"runtime"
)

type Info struct {
	System  string //系统类型 windows,linux
	GoRoot  string //go root
	NumCpu  int    //cup数量
	Version string //go 版本
	GoArch  string //cpu 构架 amd64
}

func New() {
	logs.Test(runtime.GOROOT())
	logs.Test(runtime.NumCPU())
	logs.Test(runtime.Version())
	logs.Test(runtime.NumGoroutine())
	logs.Test(runtime.NumCgoCall())
	logs.Test(runtime.GOARCH)
	logs.Test(runtime.GOOS)
	logs.Test(string(runtime.ReadTrace()))
	a := new(runtime.MemStats)
	runtime.ReadMemStats(a)
	logs.Test(a.HeapSys)
	logs.Test(a.HeapInuse)
	logs.Test(a.HeapAlloc)
	logs.Test(a.HeapIdle)
	logs.Test(a.HeapObjects)
	logs.Test(a.HeapReleased)
}

//返回引用信息
func CallInfo(n ...int) string {
	m := 1
	if len(n) != 0 {
		m = n[0]
	}
	_, path, line, _ := runtime.Caller(m)
	s := fmt.Sprint(path, " ", line, ":")
	return s
}

//runtime.FuncForPC(reflect.ValueOf(v).Pointer()).Name()
func FuncName(n int) string {
	pc, _, _, _ := runtime.Caller(n)
	return runtime.FuncForPC(pc).Name()
}

func Path(n ...int) string {
	m := 1
	if len(n) != 0 {
		m = n[0]
	}
	_, path, line, _ := runtime.Caller(m)
	return fmt.Sprint(path, " ", line, ":")
}

func PathAndName(n ...int) (string, string) {
	m := 1
	if len(n) != 0 {
		m = n[0]
	}
	pc, path, line, _ := runtime.Caller(m)
	s := fmt.Sprint(path, " ", line, ":")
	return s, runtime.FuncForPC(pc).Name()
}
